using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameStateManagement;


namespace UpChickenFar
{
    
    public class MenuScreen : GameScreen
    {

        SpriteBatch _spriteBatch;
        Texture2D _menu_back;

        Cloud _cloud1;
        Cloud _cloud2;
        Cloud _cloud3;

        Colum _col1;
        Colum _col2;

        Rotor _rotor1;
        Rotor _rotor2;

        //Menu _start_menu;
        //Menu _option_menu;
        //Menu _highscore_menu;
        //Menu _about_menu;
        //Menu _exit_menu;
        Menu[] _menu;

        const int _distance_header = 100;
        const int _distance_menu = 20;
        int _x_menu;
        float[] _speed_menu;


        ContentManager _content;
        bool _allow_handle_input = true;

        public MenuScreen()
        {
            _spriteBatch = UpChickenFar.spriteBatch;
            EnabledGestures = Microsoft.Xna.Framework.Input.Touch.GestureType.Tap;
        }

        public override void Activate(bool instancePreserved)
        {
            if (_content == null)
                _content = new ContentManager(ScreenManager.Game.Services, "Content");

            _menu_back = _content.Load<Texture2D>("Textures/Backgrounds/menuBackground");
            Texture2D _cloud1_texture = _content.Load<Texture2D>("Textures/Items/cloud1");
            Texture2D _cloud2_texture = _content.Load<Texture2D>("Textures/Items/cloud2");
            Texture2D _cloud3_texture = _content.Load<Texture2D>("Textures/Items/cloud3");

            _cloud1 = new Cloud(ScreenManager.Game, new Vector2(291, 50), _cloud1_texture, 0);
            _cloud2 = new Cloud(ScreenManager.Game, new Vector2(100, 217), _cloud2_texture, 0);
            _cloud3 = new Cloud(ScreenManager.Game, new Vector2(200, 387), _cloud3_texture, 0);

            Texture2D _col1_texture = _content.Load<Texture2D>("Textures/Items/col1");
            Texture2D _col2_texture = _content.Load<Texture2D>("Textures/Items/col2");

            _col1 = new Colum(ScreenManager.Game, new Vector2(66, 700), _col1_texture);
            _col2 = new Colum(ScreenManager.Game, new Vector2(107, 697), _col2_texture);

            Texture2D _rotor_texture = _content.Load<Texture2D>("Textures/Items/rotor");

            _rotor1 = new Rotor(ScreenManager.Game, new Vector2(81, 708), _rotor_texture, 1.1f, 0.15f);
            _rotor2 = new Rotor(ScreenManager.Game, new Vector2(120, 703), _rotor_texture, 0.9f, 0.2f);


            Texture2D[] _menu_texture = new Texture2D[5];

            //Texture2D _menu_start = _content.Load<Texture2D>("Textures/Items/menuStart");
            //Texture2D _menu_option = _content.Load<Texture2D>("Textures/Items/menuOption");
            //Texture2D _menu_highscore = _content.Load<Texture2D>("Textures/Items/menuHighScore");
            //Texture2D _menu_about = _content.Load<Texture2D>("Textures/Items/menuAbout");
            //Texture2D _menu_exit = _content.Load<Texture2D>("Textures/Items/menuExit");

            _menu_texture[0] = _content.Load<Texture2D>("Textures/Items/menuStart");
            _menu_texture[1] = _content.Load<Texture2D>("Textures/Items/menuOption");
            _menu_texture[2] = _content.Load<Texture2D>("Textures/Items/menuHighScore");
            _menu_texture[3] = _content.Load<Texture2D>("Textures/Items/menuAbout");
            _menu_texture[4] = _content.Load<Texture2D>("Textures/Items/menuExit");

            _x_menu = (ScreenManager.GraphicsDevice.Viewport.Width - _menu_texture[0].Width) / 2;

            _menu = new Menu[5];
            _speed_menu = new float[5];
            for (int i = 0; i < 5; i++)
            {
                _speed_menu[i] = 10 * (5 - i);
                _menu[i] = new Menu(ScreenManager.Game, new Vector2(-_menu_texture[i].Width, _distance_header + (_distance_menu + _menu_texture[0].Height) * i), _menu_texture[i], _speed_menu[i], _x_menu);
            }
            
        }

        public override void HandleInput(GameTime gameTime, InputState input)
        {

            if (_allow_handle_input)
            {
                _rotor1.HandleInput(gameTime, input);
                _rotor2.HandleInput(gameTime, input);

                for (int i = 0; i < 5; i++)
                {
                    _menu[i].HandleInput(gameTime, input);
                }
            }
            base.HandleInput(gameTime, input);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {

            _rotor1.Update(gameTime);
            _rotor2.Update(gameTime);

            for (int i = 0; i < 5; i++)
            {
                _menu[i].Update(gameTime);
            }
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            _spriteBatch.Draw(_menu_back, Vector2.Zero, null, Color.White);
            _spriteBatch.End();
            _cloud1.Draw(gameTime);
            _cloud2.Draw(gameTime);
            _cloud3.Draw(gameTime);
            _col2.Draw(gameTime);
            _rotor2.Draw(gameTime);
            _col1.Draw(gameTime);
            _rotor1.Draw(gameTime);

            for (int i = 0; i < 5; i++)
            {
                _menu[i].Draw(gameTime);
            }
            base.Draw(gameTime);
        }

        
    }
}
